<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TRY avoiding copy/move - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/outcome.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">TRY avoiding copy/move</h1></div></div></div>
  <p><a href="../../../reference/macros/tryv.html" class="api-reference"><code>BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr)</code></a>
 works by creating an internal uniquely
named variable which holds the value emitted by the expression. This implies that a copy
or move operation shall be performed on the object emitted (unless you are on C++ 17 or
later, which has guaranteed copy elision), which may be undesirable for your use case.</p>

<p>You can tell <code>BOOST_OUTCOME_TRY</code> to use a reference rather than a value for the internal
uniquely named variable like this:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// This refers to a Result containing an immovable object
</span><span class="c1"></span><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">Immovable</span><span class="o">&gt;</span> <span class="o">&amp;&amp;</span><span class="n">res</span><span class="p">;</span>

<span class="c1">// For when you do want to extract the value
</span><span class="c1">// This creates an auto &amp;&amp;unique = res, followed by an
</span><span class="c1">// auto &amp;&amp;v = std::move(unique).assume_value()
</span><span class="c1"></span><span class="n">BOOST_OUTCOME_TRY</span><span class="p">((</span><span class="k">auto</span> <span class="o">&amp;&amp;</span><span class="p">,</span> <span class="n">v</span><span class="p">),</span> <span class="n">res</span><span class="p">);</span>
</code></pre></div>
<p>If you don&rsquo;t care about extracting the value:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// For when you don&#39;t want to extract the value
</span><span class="c1">// This creates an auto &amp;&amp;unique = res
</span><span class="c1"></span><span class="n">BOOST_OUTCOME_TRYV2</span><span class="p">(</span><span class="k">auto</span> <span class="o">&amp;&amp;</span><span class="p">,</span> <span class="n">res</span><span class="p">);</span>
</code></pre></div>

        </div><p><small>Last revised: February 12, 2021 at 20:25:43 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/outcome.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
